package com.cskaoyan.service.wx.comment;

import com.cskaoyan.bean.MarketComment;
import com.cskaoyan.bean.MarketCommentExample;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.MarketUserExample;
import com.cskaoyan.bean.bo.wx.comment.CommentOfListBo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.wx.comment.CommentForcountVo;
import com.cskaoyan.bean.vo.wx.comment.CommentListEntityVo;
import com.cskaoyan.bean.vo.wx.comment.CommentUserInfoEntityVo;
import com.cskaoyan.mapper.MarketCommentMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Mr.Tang
 * @since 2022/09/11 17:28
 */
@Service
public class CommentsServiceImpl implements CommentsService {
    @Autowired
    MarketCommentMapper marketCommentMapper;
    @Autowired
    MarketUserMapper marketUserMapper;

    /**
     * 评论清单
     * @param commentOfListBo
     * @return
     */
    @Override
    public CommonData<CommentListEntityVo> listForComments(CommentOfListBo commentOfListBo) {
        // 开启分页
        PageHelper.startPage(commentOfListBo.getPage(),commentOfListBo.getLimit());
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        // 开启排序
        marketCommentExample.setOrderByClause("add_time desc");
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
        criteria.andValueIdEqualTo(commentOfListBo.getValueId()).andTypeEqualTo(commentOfListBo.getType());
        // 查出comment表里面的数据做封装
        if(commentOfListBo.getShowType() == 1) {   // 有图
         criteria.andHasPictureEqualTo(true);
        }


        List<MarketComment> marketComments = marketCommentMapper.selectByExample(marketCommentExample);





        PageInfo<MarketComment> marketCommentPageInfo = new PageInfo<>(marketComments);


        CommentUserInfoEntityVo commentUserInfoEntityVo = new CommentUserInfoEntityVo();
        // 拿到当前用户信息
        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = primaryPrincipal.getId();
//        Integer userId = 1;
        Date addTime = null;
        String[] picUrls = null;
        String adminContent = null;
        String content = null;

//        for (MarketComment marketComment : marketComments) {
//             userId = marketComment.getUserId();
//             addTime = marketComment.getAddTime();
//            picUrls = marketComment.getPicUrls();
//             adminContent = marketComment.getAdminContent();
//             content = marketComment.getContent();
//        }

        MarketUserExample marketUserExample = new MarketUserExample();
        MarketUserExample.Criteria criteria1 = marketUserExample.createCriteria();
        criteria1.andIdEqualTo(userId);
        List<MarketUser> marketUsers = marketUserMapper.selectByExample(marketUserExample);
        String nickname = null;
        String avatar = null;
        for (MarketUser user : marketUsers) {
            nickname  = user.getNickname();
            avatar  = user.getAvatar();
        }

        // 放入三级响应commentUserInfoEntityVo中
        commentUserInfoEntityVo.setNickName(nickname);
        commentUserInfoEntityVo.setAvatarUrl(avatar);

        List<CommentListEntityVo> commentListEntityVoList = new ArrayList<>();
        for (MarketComment marketComment : marketComments) {
            CommentListEntityVo listEntityVo = new CommentListEntityVo();
            listEntityVo.setContent(marketComment.getContent());
            listEntityVo.setUserInfo(commentUserInfoEntityVo);
            listEntityVo.setAdminContent(marketComment.getAdminContent());
            listEntityVo.setPicList(marketComment.getPicUrls());
            listEntityVo.setAddTime(marketComment.getAddTime());
            commentListEntityVoList.add(listEntityVo);
        }




        CommonData<CommentListEntityVo> commonData = new CommonData<>();
        commonData.setLimit(commentOfListBo.getLimit());
        commonData.setPage(commentOfListBo.getPage());
        commonData.setTotal((int) marketCommentPageInfo.getTotal());

        commonData.setList(commentListEntityVoList);
        commonData.setPages(marketCommentPageInfo.getPages());


        return commonData;
    }

    /**
     * 评论数量
     * @param valueId
     * @param type
     * @return
     */
    @Override
    public CommentForcountVo countForcomment(Integer valueId, Byte type) {
        // 查询所有评论
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
         criteria.andValueIdEqualTo(valueId);
         criteria.andTypeEqualTo(type);
        List<MarketComment> marketComments = marketCommentMapper.selectByExample(marketCommentExample);
        int allcount = marketComments.size();
        CommentForcountVo commentForcountVo = new CommentForcountVo();
        commentForcountVo.setAllCount(allcount);

//        Integer allcount =null;
//        long = marketCommentMapper.countByExample(marketCommentExample);

        // 查询带图片的评论
        MarketCommentExample marketCommentExample1 = new MarketCommentExample();
        MarketCommentExample.Criteria criteria1 = marketCommentExample1.createCriteria();
        criteria1.andValueIdEqualTo(valueId);
        criteria1.andTypeEqualTo(type);
        criteria1.andHasPictureEqualTo(true);
//        criteria1.andTypeEqualTo((byte) 0);
        int hasPicCount = marketCommentMapper.selectByExample(marketCommentExample1).size();
//        Integer hasPicCount = marketCommentMapper.countByExample(marketCommentExample1);
        commentForcountVo.setHasPicCount(hasPicCount);
        return commentForcountVo;
    }

    /**
     * 发表评论
     * @param marketComment
     * @param uid
     * @return
     */
    @Override
    public List<MarketComment> postForComment(MarketComment marketComment,Integer uid) {
        marketComment.setUserId(uid);
//        marketComment.setDeleted(false);
        marketComment.setAddTime(new Date());
        marketComment.setUpdateTime(new Date());
        int i = marketCommentMapper.insertSelective(marketComment);
        Integer id = marketComment.getId();
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        marketCommentExample.createCriteria().andIdEqualTo(id);
//        List<MarketComment> marketComments = marketCommentMapper.selectByExample(marketCommentExample);

//        MarketComment commentOfPostVo = marketCommentMapper.selectByPrimaryKey(id);
        List<MarketComment> marketComments = marketCommentMapper.selectByExample(marketCommentExample);
//
//        CommentOfPostVo commentOfPostVo = new CommentOfPostVo();
//        commentOfPostVo.setId(marketComments.getId());
//        commentOfPostVo.setValueId(marketComments.getValueId());
//        commentOfPostVo.setType(marketComments.getType());
//        commentOfPostVo.setContent(marketComments.getContent());
//        commentOfPostVo.setUserId(marketComments.getUserId());
//        commentOfPostVo.setHasPicture(marketComments.getHasPicture());
//        commentOfPostVo.setPicUrls(marketComments.getPicUrls());
//        commentOfPostVo.setStar(marketComments.getStar());
//        commentOfPostVo.setAddTime(marketComments.getAddTime());
//        commentOfPostVo.setUpdateTime(marketComments.getUpdateTime());
//        System.out.println(commentOfPostVo);
//        MarketComment commentOfPostVo = new MarketComment();
//        commentOfPostVo.setId(marketComments.getId());
//        commentOfPostVo.setValueId(marketComments.getValueId());
//        commentOfPostVo.setType(marketComments.getType());
//        commentOfPostVo.setContent(marketComments.getContent());
//        commentOfPostVo.setUserId(marketComments.getUserId());
//        commentOfPostVo.setHasPicture(marketComments.getHasPicture());
//        commentOfPostVo.setPicUrls(marketComments.getPicUrls());
//        commentOfPostVo.setStar(marketComments.getStar());
//        commentOfPostVo.setAddTime(marketComments.getAddTime());
//        commentOfPostVo.setUpdateTime(marketComments.getUpdateTime());
//        System.out.println(commentOfPostVo);



        return marketComments;
    }
}
